package com.jason.learn.springcloud.orderservice.dao;


import com.github.pagehelper.PageHelper;
import com.jason.learn.springcloud.orderservice.enty.BuyOrder;
import com.jason.learn.springcloud.orderservice.maper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OrderDao {

    @Autowired
    public OrderMapper orderMapper;

    public List<BuyOrder> findAll(int page, int rows){
        PageHelper.startPage(page, rows);
        return orderMapper.findAll();
    }

    public List<BuyOrder> findAllByTime(int page, int rows, Long beginTime, Long endTime){
        PageHelper.startPage(page, rows);
        return orderMapper.findAllByTime(beginTime,endTime);
    }

    public BuyOrder findOne(Long orderId, Long beginTime, Long endTime){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("orderId",orderId);
        paramMap.put("beginTime",beginTime);
        paramMap.put("endTime",endTime);
        return orderMapper.findOne(paramMap);
    }

    public BuyOrder findById(Long orderId) {
        return orderMapper.findById(orderId);
    }

    @Transactional(rollbackFor = Exception.class)
    public Long addOne(BuyOrder order){
        return orderMapper.insert(order);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(BuyOrder order){
        orderMapper.insert(order);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateOrder(BuyOrder order, int status){
        orderMapper.updateOrder(order.getOrderId(), order.getStatus(),status);
    }
}
